package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.sell.dao.ReturnOrderReviewProcessDao;
import com.wing.sell.model.entity.ReturnOrderReviewProcess;
import com.wing.sell.model.entity.ReturnOrderReviewProcessCompany;
import com.wing.sell.model.entity.ReturnOrderReviewProcess;
import com.wing.sell.model.request.ReturnOrderReviewProcessForm;
import com.wing.sell.model.response.ReturnOrderReviewProcessVO;
import com.wing.sell.service.ReturnOrderReviewProcessCompanyService;
import com.wing.sell.service.ReturnOrderReviewProcessService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 退货订单评审环节-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ReturnOrderReviewProcessServiceImpl extends ServiceImpl<ReturnOrderReviewProcessDao, ReturnOrderReviewProcess> implements ReturnOrderReviewProcessService {

    @Autowired
    private ReturnOrderReviewProcessCompanyService returnOrderReviewProcessCompanyService;

    @Override
    @Transactional(readOnly = false)
    public int add(ReturnOrderReviewProcessForm returnOrderReviewProcessForm) {

        //校验

        ReturnOrderReviewProcess returnOrderReviewProcess =new ReturnOrderReviewProcess();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ReturnOrderReviewProcess.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(returnOrderReviewProcessForm, returnOrderReviewProcess, copyOptions);

        int i = baseMapper.insert(returnOrderReviewProcess);
        if(i > 0){
            for (ReturnOrderReviewProcessCompany returnOrderReviewProcessCompany : returnOrderReviewProcessForm.getReturnOrderReviewProcessCompanyList()) {
                returnOrderReviewProcessCompany.setCreator(JwtUtils.getLoginUserId());
                returnOrderReviewProcessCompany.setCreatorName(JwtUtils.getLoginUsername());
                returnOrderReviewProcessCompany.setModifier(JwtUtils.getLoginUserId());
                returnOrderReviewProcessCompany.setModifierName(JwtUtils.getLoginUsername());
                returnOrderReviewProcessCompany.setProcessId(returnOrderReviewProcess.getId());
            }
            returnOrderReviewProcessCompanyService.saveBatch(returnOrderReviewProcessForm.getReturnOrderReviewProcessCompanyList());
        }
        return i;
    }

    @Override
    @Transactional(readOnly = false)
    public boolean update(ReturnOrderReviewProcess returnOrderReviewProcess) {
        int i = baseMapper.updateById(returnOrderReviewProcess);
        if(i > 0){
            List<ReturnOrderReviewProcessCompany> addCompanys = new ArrayList<>();
            List<ReturnOrderReviewProcessCompany> updCompanys = new ArrayList<>();
            List<Long> delCompanys = new ArrayList<>();
            List<Long> ids = new ArrayList<>();
            for (ReturnOrderReviewProcessCompany returnOrderReviewProcessCompany : returnOrderReviewProcess.getReturnOrderReviewProcessCompanyList()) {
                if(returnOrderReviewProcessCompany.getId() == null){
                    returnOrderReviewProcessCompany.setCreator(JwtUtils.getLoginUserId());
                    returnOrderReviewProcessCompany.setCreatorName(JwtUtils.getLoginUsername());
                    returnOrderReviewProcessCompany.setModifier(JwtUtils.getLoginUserId());
                    returnOrderReviewProcessCompany.setModifierName(JwtUtils.getLoginUsername());
                    returnOrderReviewProcessCompany.setProcessId(returnOrderReviewProcess.getId());
                    addCompanys.add(returnOrderReviewProcessCompany);
                }else{
                    ids.add(returnOrderReviewProcessCompany.getId());
                    updCompanys.add(returnOrderReviewProcessCompany);
                }
            }
            // 查询旧数据
            List<ReturnOrderReviewProcessCompany> oldCompanys = returnOrderReviewProcessCompanyService.list(new QueryWrapper<ReturnOrderReviewProcessCompany>().eq("process_id", returnOrderReviewProcess.getId()));
            for (ReturnOrderReviewProcessCompany oldCompany : oldCompanys) {
                if(!ids.contains(oldCompany.getId())){
                    delCompanys.add(oldCompany.getId());
                }
            }
            if(addCompanys.size() > 0){
                returnOrderReviewProcessCompanyService.saveBatch(addCompanys);
            }
            if(updCompanys.size() > 0){
                returnOrderReviewProcessCompanyService.updateBatchById(updCompanys);
            }
            if(delCompanys.size() > 0){
                returnOrderReviewProcessCompanyService.removeByIds(delCompanys);
            }
        }
        return i > 0;
    }

    @Override
    public List<ReturnOrderReviewProcessVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ReturnOrderReviewProcessVO> pageList4app(Page page, Map<String, Object> params) {
        List<ReturnOrderReviewProcessVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ReturnOrderReviewProcessVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
